//
//  MainViewController.m
//  NiuWaJiaoYu
//
//  Created by MinLison on 2017/8/31.
//  Copyright © 2017年 niuwaketang. All rights reserved.
//

#import "MainViewController.h"
#import "BaseNavigationViewController.h"
#import "NYTabBarViewController.h"

@interface MainViewController ()<IIViewDeckControllerDelegate>
@property(nonatomic, strong) NYTabBarViewController *tabbarViewController;
@end

@implementation MainViewController
- (instancetype)init
{
        if (self = [super init])
        {
                self.centerViewController = self.tabbarViewController;
                self.delegate = self;
        }
        return self;
}
- (BOOL)viewDeckController:(IIViewDeckController *)viewDeckController willOpenSide:(IIViewDeckSide)side
{
        return [self getShowingNavitagionController].viewControllers.count <= 1;
}
- (BOOL)viewDeckController:(IIViewDeckController *)viewDeckController shouldStartPanningToSide:(IIViewDeckSide)side
{
        return [self getShowingNavitagionController].viewControllers.count <= 1;
}
- (void)viewDidLoad
{
        [super viewDidLoad];
}
- (UINavigationController *)getShowingNavitagionController
{
        return self.tabbarViewController.selectedViewController;
}
- (void)addTabbarController:(__kindof UINavigationController *)vc atIndex:(NSInteger)index
{
        [self.tabbarViewController addTabBarController:vc atIndex:index];
}
- (void)popToRoot:(BOOL)animation selectedIndex:(NSInteger)index completion:(void (^ __nullable)(BOOL cancelled))completion
{
        Class loginControllerClass = NSClassFromString(@"NYFormLoginViewController");
        void(^PopController)(void) = ^(void) {
                BaseNavigationViewController *vc = self.tabbarViewController.selectedViewController;
                if ([vc isKindOfClass:[UINavigationController class]])
                {
                        if ([vc.topViewController isKindOfClass:loginControllerClass])
                        {
                                if (completion) {
                                        completion(YES);
                                }
                                return ;
                        } else {
                                [vc popToRootViewControllerAnimated:animation];
                        }
                }
                if (index >= 0 && index < self.tabbarViewController.viewControllers.count - 1) {
                        [self.tabbarViewController setSelectedIndex:index];
                }
                if (completion) {
                        completion(NO);
                }
        };
        
        void(^DismissController)(void) = ^(void) {
                /// 有 presented 的控制器
                if (self.isPresentingOther)
                {
                        if ([self.presentedViewController isKindOfClass:loginControllerClass]) {
                                if (completion) {
                                        completion(YES);
                                }
                        } else {
                                [self.presentedViewController dismissViewControllerAnimated:animation completion:^{
                                        if (self.centerViewController.isPresentingOther) {
                                                [self.centerViewController.presentedViewController dismissViewControllerAnimated:animation completion:^{
                                                        PopController();
                                                }];
                                        } else {
                                                PopController();
                                        }
                                }];
                        }
                } else {
                        PopController();
                }
        };
        if (self.openSide != IIViewDeckSideNone) {
                [self closeSide:YES completion:^(BOOL cancelled) {
                        DismissController();
                }];
        } else {
                DismissController();
        }
       
        
}
- (NYTabBarViewController *)tabbarViewController
{
        if (_tabbarViewController == nil) {
                _tabbarViewController = [[NYTabBarViewController alloc] init];
        }
        return _tabbarViewController;
}


/// MARK: - 把statusBar的控制权交给栈顶控制器
- (UIStatusBarStyle)preferredStatusBarStyle
{
        return self.centerViewController.preferredStatusBarStyle;
}

- (nullable UIViewController *)childViewControllerForStatusBarStyle
{
        return self.centerViewController;
}
- (nullable UIViewController *)childViewControllerForStatusBarHidden
{
        return self.centerViewController;
}
- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation
{
        return self.centerViewController.preferredStatusBarUpdateAnimation;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
        return self.centerViewController.supportedInterfaceOrientations;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
        return self.centerViewController.preferredInterfaceOrientationForPresentation;
}
- (BOOL)shouldAutorotate
{
        return self.centerViewController.shouldAutorotate;
}
- (BOOL)prefersStatusBarHidden
{
        return self.centerViewController.prefersStatusBarHidden;
}

@end
